﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SharpErp.Data.Service
{
    public partial class SharpErpServiceImpl : ISharpErpService
    {
        public List<Common.View.GoodsBarcodeView> GetGoodsBarcodeViews(List<int> ids)
        {
            var items = new List<Common.View.GoodsBarcodeView>();
            using(var context = new Data.Common.Context.SqliteDbContext())
            {
                var eightClassItems = context.SelectAll(context.ItemNineClasses, p => ids.Contains(p.Id));
                var parentIds = eightClassItems.Select(p => p.ParentId);
                var sevenClassItems = context.SelectAll(context.ItemEightClasses, p => parentIds.Contains(p.Id));
                foreach(var item in eightClassItems)
                {
                    var sevenItem = sevenClassItems.First(p => p.Id == item.ParentId);
                    var parentNames = sevenItem.ParentDesc?.Split(new string[] {"$$"}, StringSplitOptions.RemoveEmptyEntries);
                    if (parentNames == null) parentNames = new string[0];
                    items.Add(new Common.View.GoodsBarcodeView {
                        Code = sevenItem.Code,
                        Price = item.SalePrice,
                        Unit = sevenItem.Unit,
                        Name = parentNames.Length > 0 ? parentNames[1] : "",
                        Product = parentNames.Length > 1 ? parentNames[2] : "",
                        Width = parentNames.Length > 2 ? parentNames[3] : "",
                        Face = parentNames.Length > 3 ? parentNames[4] : "",
                        Standard = sevenItem.Standard,
                        EightClassId = item.Id,
                    });
                }
            }
            return items;
        }

        public List<Common.View.GoodsBarcodeView> GetMaterialBarcodeViews(List<int> ids)
        {
            var items = new List<Common.View.GoodsBarcodeView>();
            using (var context = new Data.Common.Context.SqliteDbContext())
            {
                var nineClassItems = context.SelectAll(context.MaterialNineClasses, p => ids.Contains(p.Id));
                var parentIds = nineClassItems.Select(p => p.ParentId);
                var eightClassItems = context.SelectAll(context.MaterialEightClasses, p => parentIds.Contains(p.Id));
                foreach (var item in nineClassItems)
                {
                    var sevenItem = eightClassItems.First(p => p.Id == item.ParentId);
                    var parentNames = sevenItem.ParentDesc?.Split(new string[] { "$$" }, StringSplitOptions.RemoveEmptyEntries);
                    if (parentNames == null) parentNames = new string[0];
                    items.Add(new Common.View.GoodsBarcodeView
                    {
                        Code = sevenItem.Code,
                        Price = item.BuyPrice,
                        Unit = sevenItem.Unit,
                        Name = parentNames.Length > 0 ? parentNames[1] : "",
                        Product = parentNames.Length > 1 ? parentNames[2] : "",
                        Width = parentNames.Length > 2 ? parentNames[3] : "",
                        Face = parentNames.Length > 3 ? parentNames[4] : "",
                        EightClassId = item.Id,
                    });
                }
            }
            return items;
        }
    }
}
